package com.ty.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Configuration
public class SetinelConfig {

    private static  HashMap  map = new HashMap();

    static {
        map.put("code", 429);
        map.put("message", "请求频繁，请稍后再试");
    }

    @PostConstruct
    public void init(){
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler(){
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t) {
                return ServerResponse.ok().body(Mono.just(map), HashMap.class);
            }
        });
    }

}
